package com.changgou.goods.controller;

import com.changgou.entity.PageResult;
import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.goods.service.AlbumService;
import com.changgou.pojo.Album;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Project: com.changgou.goods.controller
 * @Author: ShaoDi Wang
 * @Date: Created in 2019-07-23 11:00
 * @Description:
 * @Version: 1.0
 */
@RestController
@RequestMapping("/album")
public class AlbumController {

    @Autowired
    private AlbumService albumService;

    /**
     * 查询所有相册
     *
     * @return List<相册>
     */
    @GetMapping
    public Result findAll() {
        List<Album> list = albumService.findAll();
        return new Result(true, StatusCode.OK, "查询成功", list);
    }

    /**
     * 根据主键id查询相册信息
     *
     * @param id 主键id
     * @return 相册类实例
     */
    @GetMapping("/{id}")
    public Result findById(@PathVariable Long id) {
        Album album = albumService.findById(id);
        return new Result(true, StatusCode.OK, "查询成功", album);
    }

    /***
     * 新增相册数据
     * @param album 相册
     * @return Result
     */
    @PostMapping
    public Result add(@RequestBody Album album) {
        albumService.add(album);
        return new Result(true, StatusCode.OK, "添加成功");
    }

    /**
     * 根据id修改相册信息
     *
     * @param album 相册类实例
     * @param id    主键id
     * @return Result
     */
    @PutMapping("/{id}")
    public Result update(@RequestBody Album album, @PathVariable Long id) {
        album.setId(id);
        albumService.update(album);
        return new Result(true, StatusCode.OK, "修改成功");
    }

    /**
     * 根据id删除相册信息
     *
     * @param id 相册主键id
     * @return Result
     */
    @DeleteMapping(value = "/{id}")
    public Result delete(@PathVariable Long id) {
        albumService.delete(id);
        return new Result(true, StatusCode.OK, "删除成功");
    }

    /**
     * 相册列表多条件查询
     *
     * @param searchMap 查询条件集合
     * @return Result
     */
    @GetMapping(value = "/search")
    public Result findList(@RequestParam Map searchMap) {
        List<Album> list = albumService.findList(searchMap);
        return new Result(true, StatusCode.OK, "查询成功", list);
    }

    /**
     * 分页查询
     *
     * @param page 当前页码
     * @param size 每页展示多少个
     * @return 分页查询结果
     */
    @GetMapping(value = "/page/{page}/{size}")
    public Result findPage(@PathVariable Integer page,
                           @PathVariable Integer size) {
        Page<Album> pageList = albumService.findPage(page, size);
        PageResult<Album> pageResult = new PageResult<>(pageList.getTotal(),
                pageList.getResult());
        return new Result(true, StatusCode.OK, "查询成功", pageResult);
    }

    /**
     * 多条件分页查询
     * @param searchMap 多条件查询集合
     * @param page 当前页码
     * @param size 每页展示多少个
     * @return Result
     */
    @GetMapping(value = "/search/{page}/{size}")
    public Result findPage(@RequestParam Map searchMap,
                           @PathVariable Integer page,
                           @PathVariable Integer size) {
        Page<Album> pageList = albumService.findPage(searchMap, page, size);
        PageResult pageResult = new PageResult<>(pageList.getTotal(),
                pageList.getResult());
        return new Result(true, StatusCode.OK, "查询成功", pageResult);
    }
}
